import {baseUrl} from './config.js'

/**
 * url 请求地址
 * data 请求参数
 * loading是否需要loading,默认为true
 */

const get = (url, data,loading) => {
  let token = wx.getStorageSync('token');
  url = baseUrl + url;
  return new Promise((resolve,reject)=> {
    wx.showLoading({
      title: '玩命加载中。。。。',
    })
    
    wx.request({
      url,
      data,
      header: {
        'Authorization': 'Bearer ' + token
      },
      success:(res) => {
        if (res.data.code === 666) {
          resolve(res.data);
        } else {
          console.log(res.data.msg);
        }
      }, fail: (err)=> {
        console.log('请求失败信息',err);
      },complete: ()=> {
        wx.hideLoading();
      }
    })
  })
}

const post = (url, data, loading) => {
  let token = wx.getStorageSync('token');

  url = baseUrl + url;
  return new Promise((resolve, reject) => {
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url,
      method: 'post',
      data,
      header: {
        'Authorization': 'Bearer ' + token
      },
      success: (res) => {
        if (res.data.code === 666) {
          resolve(res.data);
        } else {
          console.log(res.data.msg);
        }
      }, fail: (err) => {
        console.log('请求失败信息', err);
      }, complete: () => {
        wx.hideLoading();
      }
    })
  })
}

export default {
  get,
  post
}
